<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="所属医院" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orgId">
              <a-select v-model="model.orgId" placeholder="请选择所属医院" @change="changeOrg">
                <a-select-option :value="i.id" v-for="(i, o) in hospList" :key="o">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属科室" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dept">
              <a-select v-model="model.dept" placeholder="请选择所属科室" :disabled="!model.orgId" @change="changeDept">
                <a-select-option :value="i.id" v-for="(i, o) in deptList" :key="o">{{ i.deptName }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="医生" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="doctorId">
              <a-select v-model="model.doctorId" placeholder="请选择医生" :disabled="!model.dept">
                <a-select-option :value="i.id" v-for="(i, o) in docList" :key="o">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="排班开始日期" required :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="startDate">
              <j-date placeholder="请选择排班开始日期" v-model="model.startDate" style="width: 200px;" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="排班结束日期" required :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="endDate">
              <j-date placeholder="请选择排班结束日期" v-model="model.endDate" style="width: 200px;" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="每天排班开始时间" required :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="startDayTime">
              <a-time-picker placeholder="请选择每天排班开始时间" :minuteStep="30" v-model="model.startDayTime" format="HH:mm"
                style="width: 200px;" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="每天排班结束时间" required :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="endDayTime">
              <a-time-picker placeholder="请选择每天排班结束时间" :minuteStep="30" v-model="model.endDayTime" format="HH:mm"
                style="width: 200px;" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="科室名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dept">
              <a-input v-model="model.dept" placeholder="请输入科室名称"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="医院id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orgId">
              <a-input v-model="model.orgId" placeholder="请输入医院id"></a-input>
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import moment from 'dayjs'

export default {
  name: 'WzScheduleForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      moment,
      hospList: [],
      deptList: [],
      docList: [],
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 },
      },
      labelCol1: {
        xs: { span: 12 },
        sm: { span: 8 },
      },
      wrapperCol1: {
        xs: { span: 12 },
        sm: { span: 9 },
      },
      confirmLoading: false,
      validatorRules: {
        orgId: [{ required: true, message: '请选择所属医院!' }],
        dept: [{ required: true, message: '请选择所属科室!' }],
        doctorId: [{ required: true, message: '请选择医生!' }],
        startDate: [{ required: true, message: '请选择排班开始日期!' }],
        endDate: [{ required: true, message: '请选择排班结束日期!' }],
        startDayTime: [{ required: true, message: '请选择每天排班开始时间!' }],
        endDayTime: [{ required: true, message: '请选择每天排班结束时间!' }],
      },
      url: {
        add: "/wz/schedule/wzSchedule/add",
        edit: "/wz/schedule/wzSchedule/edit",
        queryById: "/wz/schedule/wzSchedule/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    getAction('/wz/hosp/wzHospital/list', { pageNo: 1, pageSize: 999 }).then(res => {
      this.hospList = res.result.records;
    })
  },
  methods: {
    changeOrg(val) {
      getAction('/wz/hosp/wzHospDept/list', { pageNo: 1, pageSize: 999, orgId: val }).then(res => {
        this.deptList = res.result.records;
      })
    },
    changeDept(val) {
      getAction('/wz/doctor/wzDoctorInfo/list', { pageNo: 1, pageSize: 999, dept: val }).then(res => {
        this.docList = res.result.records;
      })
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(records) {
      let record = JSON.parse(JSON.stringify(records));
      record.startDayTime = record.startDate ? this.moment(record.startDate + ' ' + record.startDayTime) : null
      record.endDayTime = record.startDate ? this.moment(record.startDate + ' ' + record.endDayTime) : null
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          this.model.startDayTime = this.moment(this.model.startDayTime).format('HH:mm');
          this.model.endDayTime = this.moment(this.model.endDayTime).format('HH:mm');
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>